package model

import (
	"blog/utils/errmsg"
)

type User struct {
	Id       int    `gorm:"primary_key;auto_increment" json:"id"`
	Username string `gorm:"type:varchar(20);not null" json:"username"`
	Password string `gorm:"type:varchar(20);not null" json:"password"`
	Phone    string `gorm:"type:varchar(15);not null;unique_index" json:"phone"`
}

func CheckLogin(username string, password string) int {
	user, _ := GetUser(username)
	if username != user.Username {
		return errmsg.ERROR_USER_NOT_EXIST
	}
	if password != user.Password {
		return errmsg.ERROR_PASSWORD_WRONG
	}
	return errmsg.SUCCESS
}

func GetUser(username string) (User, error) {
	var user User
	err = db.Where("username = ?", username).First(&user).Error
	return user, err
}

func AddUser(username string, password string, phone string) (int, error) {
	user := User{Username: username, Password: password, Phone: phone}
	err = db.Create(&user).Error
	return user.Id, err
}
